// pagesEnterprise//pages/houseDetail/houseDetail.js
import ajax from '../../../utils/index'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 底部安全距离
     */
    safeArea: 0,
    fitmentCaseArr: ['精装修', '简装', '毛坯'],
    /**
     * 地图标记
     */
    markers: [],
    /**
     * 房源ID
     */
    houseId: null,
    /**
     * 房源信息对象
     */
    detailEntity: null,
    /**
     * 园区ID
     */
    parkid: null,
    /**
     * 是否查已阅读并同意看房协议 状态 0 未同意 1 已同意
     */
    agreementJudge: 0,
    /**
     * 预览状态
     */
    preview: false,
    /**
     * 分享弹出框
     */
    showShare: false
  },
  /**
   * 显示/隐藏分享框
   */
  shareEvent(event) {
    this.setData({ showShare: !(event.detail == 1) })
  },
  /**
   * 预约看房事件
   */
  apartmentEvent() {
    const { detailEntity } = this.data
    if (!detailEntity.hoseAgent || !detailEntity.hoseAgent.phone) return ajax.showToast("暂无联系方式")
    ajax.makePhoneCall(detailEntity.hoseAgent.phone)
  },

  /**
   * 地图点击事件
   */
  mapEvent() {
    const { garden } = this.data.detailEntity
    ajax.openMmap({
      lat: garden.latitude,
      lng: garden.longitude,
      address: `${garden.province}${garden.city}${garden.county}${garden.address}`
    }, 2)
  },
  /**
   * 放大房源图片
   * @param {图片下标} event 
   */
  housingPicturesEvent(event) {
    const { housingRentImage } = this.data.detailEntity
    const index = event.currentTarget.dataset.index
    const list = housingRentImage.map(item => item.image)
    wx.previewImage({ current: list[index], urls: list })
  },
  /**
   * 获取房产详情
   */
  async getDetailRequest() {
    const res = await ajax.get(`/my/house/rent/housingInfo?sourse=1&id=${this.data.houseId}`)
    const detailEntity = res.data
    const { garden } = res.data
    const markers = [{
      id: garden.id,
      latitude: garden.latitude,
      longitude: garden.longitude,
      iconPath: '/pagesEnterprise/image/punctuation.png',
      width: 14,
      height: 14
    }]
    this.data.parkid = garden.id
    this.setData({ detailEntity, markers })
  },
  /**
   * 发布房源
   */
  async submitEvent() {
    const { houseRent } = this.data.detailEntity
    houseRent.checkStatus = 0
    await ajax.post(houseRent.id ? '/my/house/rent/update' : '/my/house/rent/add', houseRent)
    await ajax.showSuccess('操作成功')
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('backEvent');
    wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({ withShareTicket: true, menus: ['shareAppMessage', 'shareTimeline'] })
    console.log('onLoad', options)
    app.globalData.shareId = options.shareId || null
    if (options.scene) {
      const entity = ajax.windowSearch(options.scene)
      options.houseId = entity.id
      app.globalData.shareId = entity.shareId || null
    }
    if (options.detail) {
      const detailEntity = JSON.parse(unescape(options.detail))
      return this.setData({ preview: true, detailEntity })
    }
    this.data.houseId = options.houseId
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!this.data.detailEntity && this.data.houseId) this.getDetailRequest()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 朋友圈分享
   */
  onShareTimeline: function () {
    const { garden, housingAssess, houseRent } = this.data.detailEntity
    const userInfo = wx.getStorageSync('userInfo')
    return {
      title: `${garden.gardenName} - ${housingAssess.houseArea}㎡`,
      query: `id=${houseRent.id}&shareId=${userInfo.id}`,
      imageUrl: houseRent.indexImage,
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const { garden, housingAssess, houseRent } = this.data.detailEntity
    const user = wx.getStorageSync('userInfo')
    return {
      title: `${garden.gardenName} - ${housingAssess.houseArea}㎡`,
      imageUrl: houseRent.indexImage,
      path: `/pages/park/house/detail?id=${this.data.houseId}&shareUserId=${user.id}`
    }
  }
})